package com.agriculture.competition.controller;

import com.agriculture.common.core.web.controller.BaseController;
import com.agriculture.common.core.web.domain.AjaxResult;
import com.agriculture.common.core.web.page.TableDataInfo;
import com.agriculture.common.log.annotation.Log;
import com.agriculture.common.log.enums.BusinessType;
import com.agriculture.common.security.annotation.RequiresPermissions;
import com.agriculture.competition.domain.JaCompetitionManage;
import com.agriculture.competition.domain.JaFile;
import com.agriculture.competition.domain.vo.CompetitionGroupVo;
import com.agriculture.competition.domain.vo.JaCompetitionManageVo;
import com.agriculture.competition.domain.vo.JaCompetitionScoreVo;
import com.agriculture.competition.domain.vo.JaCompetitionSolutionVo;
import com.agriculture.competition.service.IJaCompetitionManageService;
import com.alibaba.fastjson2.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 比赛管理Controller
 *
 * @author agriculture
 * @date 2024-11-12
 */
@RestController
@RequestMapping("/manage")
public class JaCompetitionManageController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(JaCompetitionManageController.class);
    @Autowired
    private IJaCompetitionManageService jaCompetitionManageService;

    /**
     * 查询比赛管理列表
     */
    @RequiresPermissions("competition:manage:list")
    @GetMapping("/list")
    public TableDataInfo list(JaCompetitionManageVo jaCompetitionManage) {
        startPage();
        List<JaCompetitionManageVo> list = jaCompetitionManageService.selectJaCompetitionManageList(jaCompetitionManage);
        return getDataTable(list);
    }

    /**
     * 导出比赛管理列表
     */
//    @RequiresPermissions("competition:manage:export")
//    @Log(title = "比赛管理", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, JaCompetitionManageVo jaCompetitionManage) {
//        List<JaCompetitionManageVo> list = jaCompetitionManageService.selectJaCompetitionManageList(jaCompetitionManage);
//        ExcelUtil<JaCompetitionManage> util = new ExcelUtil<JaCompetitionManage>(JaCompetitionManage.class);
//        util.exportExcel(response, list, "比赛管理数据");
//    }

    /**
     * 获取比赛管理详细信息
     */
    //@RequiresPermissions("competition:manage:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jaCompetitionManageService.selectJaCompetitionManageById(id));
    }

    /**
     * 新增比赛管理
     */
    // @RequiresPermissions("competition:manage:add")
    @Log(title = "比赛管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JaCompetitionManage jaCompetitionManage) {
        log.info("insertJaCompetitionManage:" + JSON.toJSONString(jaCompetitionManage));
        return toAjax(jaCompetitionManageService.insertJaCompetitionManage(jaCompetitionManage));
    }

    /**
     * 修改比赛管理
     */
    // @RequiresPermissions("competition:manage:edit")
    @Log(title = "比赛管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JaCompetitionManage jaCompetitionManage) {
        log.info("updateJaCompetitionManage:" + JSON.toJSONString(jaCompetitionManage));
        return toAjax(jaCompetitionManageService.updateJaCompetitionManage(jaCompetitionManage));
    }

    /**
     * 删除比赛管理
     */
    // @RequiresPermissions("competition:manage:remove")
    @Log(title = "比赛管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(jaCompetitionManageService.deleteJaCompetitionManageByIds(ids));
    }

    /**
     * 查比赛文件
     *
     * @param id
     * @return
     */
    // @RequiresPermissions("competition:manage:queryFiles")
    @GetMapping("/queryFiles")
    public AjaxResult queryFiles(@RequestParam("id") Long id, @RequestParam("flag") String flag) {
        return success(jaCompetitionManageService.queryFiles(id, flag));
    }


    /**
     * 查询某个比赛下所有人的方案
     *
     * @param id
     * @return
     */
    // @RequiresPermissions("competition:manage:queryScore")
    @GetMapping("/queryScore")
    public AjaxResult queryScore(@RequestParam("id") Long id) {
        return success(jaCompetitionManageService.queryScore(id));
    }


    /**
     * 查询比赛下所有的人的方案得分
     *
     * @param id
     * @return
     */
    // @RequiresPermissions("competition:manage:queryScoreResult")
    @GetMapping("/queryScoreResult")
    public AjaxResult queryScoreResult(@RequestParam("id") Long id) {
        return success(jaCompetitionManageService.queryScoreResult(id));
    }

    /**
     * 比赛结果
     *
     * @param id
     * @return
     */
    @GetMapping("/queryScoreFinal")
    public AjaxResult queryScoreFinal(@RequestParam("id") Long id) {
        return success(jaCompetitionManageService.queryScoreFinal(id));
    }

    @GetMapping("/queryScoreFinalResult")
    public AjaxResult queryScoreFinalResult(@RequestParam("id") Long id) {
        return success(jaCompetitionManageService.queryScoreFinalResult(id));
    }

    /**
     * 参加比赛
     *
     * @param vo
     * @return
     */
    //@RequiresPermissions("competition:manage:participateInCompetition")
    @PostMapping("/participateInCompetition")
    public AjaxResult participateInCompetition(@RequestBody CompetitionGroupVo vo) {
        return success(jaCompetitionManageService.participateInCompetition(vo));
    }

    /**
     * 我的比赛
     *
     * @param
     * @return
     */
    // @RequiresPermissions("competition:manage:selfCompetition")
    @GetMapping("/selfCompetition")
    public TableDataInfo selfCompetition(JaCompetitionManageVo jaCompetitionManage) {
        startPage();
        List<JaCompetitionManageVo> jaCompetitionManageVos = jaCompetitionManageService.selfCompetition(jaCompetitionManage);
        return getDataTable(jaCompetitionManageVos);
    }

    /**
     * 提交方案
     *
     * @param jaCompetitionSolutionVo
     * @return
     */
    // @RequiresPermissions("competition:manage:submitSolution")
    @PostMapping("/submitSolution")
    public AjaxResult submitSolution(@RequestBody JaCompetitionSolutionVo jaCompetitionSolutionVo) {
        if (jaCompetitionSolutionVo.getCompetitionId() == null) {
            throw new RuntimeException("比赛competitionId不能为空");
        }
        if (jaCompetitionSolutionVo.getModelId() == null) {
            throw new RuntimeException("比赛ModelId不能为空");
        }
        return success(jaCompetitionManageService.submitSolution(jaCompetitionSolutionVo));
    }


    /**
     * 比赛给每个学生评分保存
     *
     * @param competitionGroupVo
     * @return
     */
    //@RequiresPermissions("competition:manage:saveScore")
    @PostMapping("/saveScore")
    public AjaxResult saveScore(@RequestBody JaCompetitionScoreVo competitionGroupVo) {
        return success(jaCompetitionManageService.saveScore(competitionGroupVo));
    }

    /**
     * 给最终结果评分
     *
     * @param jaCompetitionScoreVo
     * @return
     */
    // @RequiresPermissions("competition:manage:saveScoreResult")
    @PostMapping("/saveScoreResult")
    public AjaxResult saveScoreResult(@RequestBody JaCompetitionScoreVo jaCompetitionScoreVo) {
        return success(jaCompetitionManageService.saveScoreResult(jaCompetitionScoreVo));
    }


    //@RequiresPermissions("competition:manage:queryCompetitionByStatus")
    @GetMapping("/queryCompetitionByStatus")
    public AjaxResult queryCompetitionByStatus() {
        return success(jaCompetitionManageService.queryCompetitionByStatus());
    }


    /**
     * 获取比赛参赛人数
     *
     * @param id
     * @return
     */
    @GetMapping("/queryCompetitionCount")
    public AjaxResult queryCompetitionCount(@RequestParam("id") Long id) {
        return success(jaCompetitionManageService.queryCompetitionCount(id));
    }

    @PostMapping("/saveCompetitionFile")
    public AjaxResult saveCompetitionFile(@RequestBody List<JaFile> jaFile) {
        return success(jaCompetitionManageService.saveCompetitionFile(jaFile));
    }

    @GetMapping("/deleteFile")
    public AjaxResult deleteFile(@RequestParam("id") Long id) {
        return success(jaCompetitionManageService.deleteFile(id));
    }

    @GetMapping("/deleteModelFile")
    public AjaxResult deleteModelFile(@RequestParam("id") Long id) {
        return success(jaCompetitionManageService.deleteModelFile(id));
    }


    /**
     * 查询评分老师列表
     */
    @GetMapping("/queryTeacherList")
    public AjaxResult queryTeacherListScore() {
        return success(jaCompetitionManageService.queryTeacherList());
    }


    /**
     * 查询评分老师列表
     */
    @GetMapping("/queryAllList")
    public AjaxResult queryAllList(@RequestParam("userName") String userName) {
        return success(jaCompetitionManageService.queryAllList(userName));
    }


    /**
     * 查比赛人所在组员信息
     *
     * @param id
     * @return
     */
    @GetMapping("/queryGroupUser")
    public AjaxResult queryGroupUser(@RequestParam("id") Long id) {
        return success(jaCompetitionManageService.queryGroupUser(id));
    }

    /**
     * 校验登陆人是否能参赛
     *
     * @param id
     * @return
     */
    @GetMapping("/checkIsCompetition")
    public AjaxResult checkIsCompetition(@RequestParam("id") Long id) {
        return success(jaCompetitionManageService.checkIsCompetition(id));
    }

    @GetMapping("/queryLoginUserGroup")
    public AjaxResult queryLoginUserGroup() {
        return success(jaCompetitionManageService.queryLoginUserGroup());
    }
}
